package com.cq.hd.app.controller;

import com.cq.hd.common.annotation.LoginUser;
import com.cq.hd.common.entity.AppLoginUser;
import com.cq.hd.common.entity.BusinessInfo;
import com.cq.hd.common.enums.biz.PayTypeEnum;
import com.cq.hd.common.pagination.Page;
import com.cq.hd.common.response.Result;
import com.cq.hd.common.response.Throw;
import com.cq.hd.member.api.TbMemberCardApi;
import com.cq.hd.member.api.dto.*;
import com.cq.hd.member.api.vo.AppMemberCardPageVo;
import com.cq.hd.member.api.vo.AppMemberCardPayVo;
import com.cq.hd.order.api.dto.OrderPayQueryDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

@RestController
@RequestMapping("/merchantCard")
@Api(tags = "火动次卡相关接口")
public class MemberCardController {

    @Autowired
    private TbMemberCardApi memberCardApi;

    @ApiOperation(value = "次卡购买列表")
    @ApiResponse(response = AppMemberCardPageVo.class, code = 200, message = "接口返回对象参数")
    @GetMapping("/page")
    public Result<Page<AppMemberCardPageVo>> pageAppMemberCard(AppMemberCardPageDto appMemberCardPageDto) {
        return memberCardApi.pageAppMemberCard(appMemberCardPageDto);
    }

    // 创建充值订单，返回充值订单号
    @ApiOperation(value = "创建订单，返回充值订单号")
    @ApiImplicitParam(name = "appMemberCardCreateDto", value = "实体", required = true, paramType = "body", dataType = "AppMemberCardCreateDto")
    @PostMapping("/createOrder")
    public Result<String> createOrder(@RequestBody AppMemberCardCreateDto appMemberCardCreateDto,
                                      @ApiIgnore @LoginUser AppLoginUser appLoginUser) {
        Integer payType = appMemberCardCreateDto.getPayType();
        if (payType == null || PayTypeEnum.typeOf(payType) == null) {
            Throw.isBusinessException("请选择支付方式");
        }

        if (!PayTypeEnum.WX.getValue().equals(payType)) {
            // 网银对公打款，返回商家后台访问地址
            Throw.isBusinessException("请前往PC端登录商家后台进行操作");
        }

        BusinessInfo businessInfo = appLoginUser.getUserInfo().getBusinessInfo();
        if (businessInfo != null) {
            appMemberCardCreateDto.setBusinessId(businessInfo.getId());
        }
        appMemberCardCreateDto.setUserId(appLoginUser.getUserId());
        return memberCardApi.createAppMemberCardOrder(appMemberCardCreateDto);
    }

    @ApiOperation(value = "立即支付，唤起第三方支付")
    @ApiImplicitParam(name = "appMemberCardPayDto", value = "实体", paramType = "body", dataType = "AppMemberCardPayDto")
    @ApiResponse(response = AppMemberCardPayVo.class, code = 200, message = "接口返回对象参数")
    @PostMapping("/pay")
    public Result<AppMemberCardPayVo> pay(@RequestBody AppMemberCardPayDto appMemberCardPayDto, @ApiIgnore @LoginUser AppLoginUser appLoginUser) {
        BusinessInfo businessInfo = appLoginUser.getUserInfo().getBusinessInfo();
        if (businessInfo != null) {
            appMemberCardPayDto.setBusinessId(businessInfo.getId());
        }
        appMemberCardPayDto.setUserId(appLoginUser.getUserId());
        return memberCardApi.payAppMemberCardOrder(appMemberCardPayDto);
    }

    @ApiOperation(value = "查单（只要唤醒了第三方支付，关闭第三方支付页面之后，不管是成功还是失败，都要调用）")
    @ApiImplicitParam(name = "appMemberCardPayQueryDto", value = "实体", paramType = "body", dataType = "AppMemberCardPayQueryDto")
    @PostMapping("/queryOrderPay")
    public Result<Boolean> queryOrderPay(@RequestBody AppMemberCardPayQueryDto appMemberCardPayQueryDto, @ApiIgnore @LoginUser AppLoginUser appLoginUser) {
        BusinessInfo businessInfo = appLoginUser.getUserInfo().getBusinessInfo();
        if (businessInfo != null) {
            appMemberCardPayQueryDto.setBusinessId(businessInfo.getId());
        }
        appMemberCardPayQueryDto.setUserId(appLoginUser.getUserId());
        return memberCardApi.queryOrderPay(appMemberCardPayQueryDto);
    }


}
